import os
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
plt.rcParams['axes.unicode_minus'] = False    # 用于正常显示负号

spath='/coin/neutral/strategy/baowincup/bwb3_v1.8/中性策略回测框架_V1.8/data/单因子遍历结果/'
files=sorted(os.listdir(spath))
for fl in files:
    if '.csv' not in fl:
        continue
    # 加载数据
    path=spath+fl
    print(path)
    df = pd.read_csv(path)  # 请将 'path_to_your_file.csv' 替换为您的文件路径
    
    df['年化收益']=df['年化收益'].str.replace('%', '').astype(float) / 100
    df['最大回撤']=df['最大回撤'].str.replace('%', '').astype(float) / 100
    df['年化回撤比'] = -df['年化收益'] / df['最大回撤']

    cols=sorted(df["datename"].unique())
    plt.clf()
    plt.figure(figsize=(16,8))
    plt.subplots_adjust(left=0.04, bottom=0.08, right=0.96, top=0.9, wspace=0.23, hspace=0.25)
    plt.suptitle(f"factor:{df['factor_1'].iloc[0]}_filter:({df['filter_1'].iloc[0]},{df['fcycle_1'].iloc[0]})_{df['持仓周期'].iloc[0]}_参数平原", fontsize=16)

    index=0
    for col in cols:
        df_=df[df['datename']==col]
        ax1 = plt.subplot(3,3,1+index*3)
        ax1.bar(df_['cycle_1'],df_['年化收益'], width=4, align='center', label='年化收益')
        ax1.set_title(col)
        ax2 = plt.subplot(3,3,2+index*3)
        ax2.bar(df_['cycle_1'],df_['最大回撤'], width=4, align='center', label='最大回撤')
        ax2.set_title(col)
        ax3 = plt.subplot(3,3,3+index*3)
        ax3.bar(df_['cycle_1'],df_['年化回撤比'], width=4, align='center', label='年化回撤比')
        ax3.set_title(col)
        ax1.legend(loc='upper right')
        ax2.legend(loc='upper right')
        ax3.legend(loc='upper right')
        index+=1
    plt.savefig(path.replace('.csv','.png'))
    plt.close('all')
    #break
